﻿package com.air.swfmessengergui
{
	
import flash.display.NativeWindow;

import flash.display.Graphics;
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageQuality;
import flash.display.StageScaleMode;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.HTTPStatusEvent;

import flash.net.URLLoader;
import flash.net.URLRequest;

import flash.system.Capabilities;

import flash.filters.GlowFilter;
import flash.filters.DropShadowFilter;

import com.air.swfmessengergui.menu.MenuPrincipal;
import com.air.swfmessengergui.display.publicidade.GoogleAdsense;

public class SWFMessenger_gui extends Sprite
{
	//private variaveis
	private var window:NativeWindow;
	private var maximized:Boolean = false;
	private var menuPrincipal:MenuPrincipal;
	private var language:XML;
	
	//public variaveis
	var base:Sprite;
	
	//valores de margens
	public var marginLeft:int = 3;
	public var marginRight:int = 6;
	public var marginTop:int = 3;
	public var marginBotton:int = 6;
	public var shadow:Boolean = true;
	public var shadowColor:uint = 0x000020;
	public var shadowSize:uint = 8;
	
	public function SWFMessenger_gui () {
		window = stage.nativeWindow;
		
		//definicoes
		stage.scaleMode = StageScaleMode.NO_SCALE;
		stage.align = StageAlign.TOP_LEFT;
		
		window.width = width;
		window.height = height;
		window.addEventListener(Event.RESIZE, redraw);
		window.title = "SWFMessenger 1.0_alpha";		
		
		try {
			var language:URLLoader = new URLLoader();
			language.addEventListener( Event.COMPLETE, start );
			language.load( new URLRequest( "resources/languages/" + Capabilities.language + ".xml" ) );
		} catch (e:Error) {
			
		}
		
		
		//contextMenu = menuPrincipal.context;

	}
	
	private function start(e:Event):void {
		language = new XML(e.target.data);
		menuPrincipal = new MenuPrincipal(stage,language.menu[0]);
		preparaSkin();
		var google:GoogleAdsense = new GoogleAdsense();
		addChild(google);
	}
	
	public function preparaSkin():void {
		bFecha.addEventListener(MouseEvent.CLICK, closeApp);
		bFecha.addEventListener(MouseEvent.MOUSE_OVER, overEfect);
		bFecha.addEventListener(MouseEvent.MOUSE_OUT, outEfect);
		bMin.addEventListener(MouseEvent.CLICK, minApp);
		bMin.addEventListener(MouseEvent.MOUSE_OVER, overEfect);
		bMin.addEventListener(MouseEvent.MOUSE_OUT, outEfect);
		bRest.addEventListener(MouseEvent.CLICK, resApp);
		bRest.addEventListener(MouseEvent.MOUSE_OVER, overEfect);
		bRest.addEventListener(MouseEvent.MOUSE_OUT, outEfect);
		
		topEsq.contextMenu = topMeio.contextMenu = topDir.contextMenu = menuPrincipal.context;
		meioEsq.contextMenu = meioDir.contextMenu = menuPrincipal.context;
		baixoEsq.contextMenu = baixoMeio.contextMenu = baixoDir.contextMenu = menuPrincipal.context;
		
		redraw(null);
		
		//if shadown
		if (shadow) {
			var sombra:DropShadowFilter = new DropShadowFilter( marginTop/2, 45, shadowColor, .75, shadowSize,shadowSize, 1,2, false,false,false);
			//var sombra:GlowFilter = new GlowFilter( shadowColor, 1, shadowSize, shadowSize, 1,   2,false,false);
			filters = [sombra];
		}
	}
	
	
	//Redesenha a tela principal, dando stretch nos componentes meiantes	
	public function redraw(e:Event):void {
		var mT:int = maximized ? 0 : marginTop;
		var mB:int = maximized ? 0 : marginBotton;
		var mL:int = maximized ? 0 : marginLeft;
		var mR:int = maximized ? 0 : marginRight;
		
		//remonta os objetos da janela
		topEsq.x = mL;
		topEsq.y = mT;
		topMeio.x = topEsq.x+topEsq.width;
		topMeio.y = mT;
		topMeio.width = window.width - mL-topEsq.width-topDir.width-mR;
		topDir.x = topMeio.x+topMeio.width;
		topDir.y = mT;
		meioEsq.x = mL;
		meioEsq.y = topEsq.y+topEsq.height;
		meioEsq.height = window.height - mT-topEsq.height-baixoEsq.height-mB;
		content.x = meioEsq.x+meioEsq.width;
		content.y = meioEsq.y;
		content.width = topMeio.width;
		content.height = meioEsq.height;
		meioDir.x = content.x+content.width;
		meioDir.y = meioEsq.y;
		meioDir.height = meioEsq.height;
		baixoEsq.x = mL;
		baixoEsq.y = meioEsq.y+meioEsq.height;
		baixoMeio.x = baixoEsq.x+baixoEsq.width;
		baixoMeio.y = baixoEsq.y;
		baixoMeio.width = content.width;
		baixoDir.x = baixoMeio.x + baixoMeio.width;
		baixoDir.y = baixoEsq.y;
		
		bFecha.x = topDir.x-bFecha.width;
		bFecha.y = topDir.y+4;
		bRest.x = bFecha.x-3-bRest.width;
		bRest.y = bFecha.y;
		bMin.x = bRest.x-3-bMin.width;
		bMin.y = bFecha.y;
		
		ico.x = mL+4;
		ico.y = mT+4;
	}
	
	//fecha a bagaça
	private function closeApp(e:Event):void {
		window.close();
	}
	
	//minimiza
	private function minApp(e:Event):void {
		window.minimize();
	}
	
	//minimiza
	private function resApp(e:Event):void {
		if (maximized) {
			window.restore();
			maximized = false;
		} else {
			window.maximize();
			maximized = true;
		}
	}
	
	
	
	
	//efeitos de mouse
	private function overEfect(e:MouseEvent):void {
		var g:GlowFilter = new GlowFilter( 0xffffaa, 1, 6,6,2,  1,true /* inner */,false);
		e.target.filters = [g];
	}
	
	private function outEfect(e:MouseEvent):void {
		e.target.filters = null;
	}
	
}
}